package com.godpaper.as9.bussiness.delegates
{
	import com.godpaper.as9.managers.ProgressBarManager;
	
	import mx.managers.CursorManager;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.remoting.RemoteObject;

	public class DelegateBase implements IResponder
	{
		protected var responder:IResponder;
		protected var service:RemoteObject;
		
		public function DelegateBase(responder:IResponder)
		{
			this.responder = responder;
		}
		//CRUD
		public function create(operation:String, ...args):void
		{
			var arguments:Array = new Array();
			for(var i:int=0;i<args.length;i++)
			{
				arguments[i] = args[i];
			}
			var token:AsyncToken = service.getOperation(operation).send(arguments);
			token.addResponder(this);
		}
		
		public function read(operation:String,arguments:Object):void
		{
			/* var arguments:Array = new Array();
			for(var i:int=0;i<args.length;i++)
			{
				arguments[i] = args[i];
			} */
			var token:AsyncToken = service.getOperation(operation).send(arguments);
			token.addResponder(this);
		}
		
		public function update(operation:String, ...args):void
		{
			var arguments:Array = new Array();
			for(var i:int=0;i<args.length;i++)
			{
				arguments[i] = args[i];
			}
			var token:AsyncToken = service.getOperation(operation).send(arguments);
			token.addResponder(this);
		}
		
		public function delete_(operation:String, ...args):void
		{
			var arguments:Array = new Array();
			for(var i:int=0;i<args.length;i++)
			{
				arguments[i] = args[i];
			}
			var token:AsyncToken = service.getOperation(operation).send(arguments);
			token.addResponder(this);
		}
		//result or fault
		public function result(data:Object):void
		{
			responder.result(data);
			CursorManager.removeBusyCursor();
			ProgressBarManager.removeProgressBar();
		}
		
		public function fault(info:Object):void
		{
			responder.fault(info);
			CursorManager.removeBusyCursor();
			ProgressBarManager.removeProgressBar();
		}
		
	}
}